package com.ml.mall.service.payinfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.ipay.Ipay88CallBackDto;
import com.ml.mall.bean.req.payment.PaymentRecordQueryAllDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.payinfo.SysPaymentRecord;
import com.ml.mall.service.payinfo.SysPaymentRecordService;
import com.ml.mall.mapper.SysPaymentRecordMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class SysPaymentRecordServiceImpl extends ServiceImpl<SysPaymentRecordMapper, SysPaymentRecord>
    implements SysPaymentRecordService{

    @Override
    public boolean saveByCallBackDto(Ipay88CallBackDto dto) {
        SysPaymentRecord sysPaymentRecord = new SysPaymentRecord();
        sysPaymentRecord.setPayId(Long.valueOf(dto.getRefNo()));
        //paymentId小于250是B2C否则则是B2B
        sysPaymentRecord.setGateway(dto.getPaymentId() < 250 ? CommonConsts.GATEWAY_TYPE_B2B : CommonConsts.GATEWAY_TYPE_B2C);
        //根据支付结果回调参数确定支付状态
        switch (dto.getStatus()){
            case CommonConsts.IPAY88_PAYMENT_FAILED: {
                if(dto.getErrDesc() != null && dto.getErrDesc().contains("Expired")){
                    sysPaymentRecord.setState(CommonConsts.PAYMENT_RECORD_STATE_EXPIRED);
                }else{
                    sysPaymentRecord.setState(CommonConsts.PAYMENT_RECORD_STATE_FAILED);
                }
                break;
            }
            case CommonConsts.IPAY88_PAYMENT_SUCCESSFUL: {

                sysPaymentRecord.setState(CommonConsts.PAYMENT_RECORD_STATE_SUCCESS);
                break;
            }
            case CommonConsts.IPAY88_PAYMENT_PADDING:{
                sysPaymentRecord.setState(CommonConsts.PAYMENT_RECORD_STATE_PENDING);
                break;
            }
        }
        return save(sysPaymentRecord);
    }

    @Override
    public List<SysPaymentRecord> queryAll(PaymentRecordQueryAllDto dto) {
        LambdaQueryWrapper<SysPaymentRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPaymentRecord::getPayId, dto.getOid());
        return list(queryWrapper);
    }
}




